﻿<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="ItemConhecimentoUsuario.ascx.cs"
    Inherits="RzCorportativo.Views.ItemConhecimentoUsuario" %>
<%--<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />--%>
<div class="accordion" id="accordion2" runat="server">
    <%this.ForEachAreas((area) =>
      {%>
    <div class="accordion-group">
        <div class="accordion-heading">
            <a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion2" href="#<%=area.AreaId.ToString()%>">
                <%=area.Nome.ToString()%>
            </a>
        </div>
        <div id="<%=area.AreaId.ToString()%>" class="accordion-body collapse" style="height: 0px">
            <%--<div id="<%=area.AreaId.ToString()%>" class="accordion-body collapse in">--%>
            <div class="accordion-inner">
                <%
          Action<RzCorportativo.Lib.ItemAreaConhecimento> funcPar = null;
                %>
                <%funcPar = (item) =>
                      { %>
                <div class="controls controls-row">
                    <label class="checkbox" style="width: auto">
                        <%if (this.IsChecked(item))
                          { %>
                        <input type="checkbox" name="item<%=item.Nome.ToString()%>" value="<%=item.ItemAreaConhecimentoId.ToString()%>"
                            user-item="true" checked="checked" />
                        <%=item.Nome.ToString()%>
                        <%}
                          else
                          { %>
                        <input type="checkbox" name="item<%=item.Nome.ToString()%>" value="<%=item.ItemAreaConhecimentoId.ToString()%>"
                            user-item="true" />
                        <%=item.Nome.ToString()%>
                        <%} %>
                    </label>
                </div>
                <%};%>
                <div class="row-fluid">
                    <div class="span4">
                        <%area.Itens.Take((int)(area.Itens.Count % 2 > 0 ? Math.Floor((decimal)(area.Itens.Count / 2)) + 1 : area.Itens.Count / 2)).OrderBy(n => n.Nome).ToList().ForEach((item) =>
                      {
                          funcPar(item);
                      });%>
                    </div>
                    <div class="span4">
                        <%area.Itens.Skip((int)(area.Itens.Count % 2 > 0 ? Math.Floor((decimal)(area.Itens.Count / 2)) + 1 : area.Itens.Count / 2)).OrderBy(n => n.Nome).ToList().ForEach((item) =>
                      {
                          funcPar(item);
                      });%>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <%});%>
</div>
<script type="text/javascript">
    $(function () {
        $("input[user-item='true']").change(function () {
            var val = $(this).val();
            var item = new ItemConhecimento();
            item.ItemAreaConhecimentoId = parseInt(val, 10);
            item.ItemAreaConhecimentoId = isNaN(item.ItemAreaConhecimentoId) ? 0 : item.ItemAreaConhecimentoId;
            if ($(this).is(":checked"))
                item.Salvar();
            else
                item.Excluir();
        });
    });

    function ItemConhecimento() {
        this.ItemAreaConhecimentoId = 0;
        this.AreaId = 0;
        this.Nome = "";
        this.Ativo = true;
        this.Salvar = function () {
            var json = JSON.stringify(this);
            current = this;
            __post("Views/Cadastros/Curriculo.aspx/AddItemUsuario", "{ item :" + json + " }", function (item) {
            });
        }
        this.Excluir = function () {
            var json = JSON.stringify(this);
            var current = this;
            __post("Views/Cadastros/Curriculo.aspx/RemoveItemUsuario", "{ item :" + json + " }", function (item) {
            });

        }
        var __post = function (url, parans, success, error) {
            var alerta = new Alert();
            alerta.Type = AlertType.Error;
            alerta.Name = "erroGeral";
            $.ajax({
                url: eS.UrlPadrao + url,
                type: "post",
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                data: parans,
                success: function (e) {
                    if (e.d && e.d.AlertType != undefined) {
                        alerta.Type = e.d.AlertType;
                        for (var i in e.d.Messages) {
                            alerta.AddMessage(e.d.Messages[i]);
                        }
                        if (e.d.Show && e.d.Show == true) {
                            alerta.Show();
                        }
                    } else {
                        if (e.responseText) {
                            alerta.Type = 2;
                            alerta.AddMessage(e.responseText);
                        } else {
                            alerta.Type = 1;
                            alerta.AddMessage("Retorno inválido, o objeto retornado não está em um formato correto")
                        }
                        alerta.Show();
                    }
                    if (alerta.Type == AlertType.Success && success && typeof success == "function") {
                        success.call(this, e);
                    }
                },
                error: function (e) {
                    alerta.Type = 1;
                    alerta.AddMessage(e.responseText);
                    alerta.Show();
                    if (error && typeof error == "function") {
                        error.call(this, e);
                    }
                }
            });
        }
    }
</script>
